package ar.edu.itba.it.cg.grupo01.menu;

import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.GameMenuButton;
import org.fenggui.IWidget;
import org.fenggui.Label;
import org.fenggui.Slider;
import org.fenggui.Widget;
import org.fenggui.binding.render.lwjgl.LWJGLBinding;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.ISliderMovedListener;
import org.fenggui.event.SliderMovedEvent;
import org.fenggui.layout.RowLayout;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;
import org.fenggui.util.Spacing;

import ar.edu.itba.it.cg.grupo01.game.BaseGame;
import ar.edu.itba.it.cg.grupo01.game.GameMode;
import ar.edu.itba.it.cg.grupo01.game.action.score.Score;
import ar.edu.itba.it.cg.grupo01.game.action.score.Scores;

import com.jme.input.MouseInput;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.shape.Quad;
import com.jme.system.DisplaySystem;
import com.jmex.scene.TimedLifeController;

public class Menu extends Quad implements Stateable  {

    private boolean first = true;
    private boolean active;
    private boolean started = false;

    private boolean reset = false;

    // Feng GUI
    private Display display = null;

    private FengJMEInputHandler input = null;

    private Container menu;
    // Menu
    private GameMenuButton play, options, highscores, help, quit, menutit;
    private GameMenuButton resume, newgame;

    private GameMenuButton gamemode, timeattack, flags;
    private GameMenuButton optionstit, music, fx;
    private GameMenuButton controls, keys;
    private GameMenuButton highscorestit;
    private Slider musicVolume, fxVolume;

    private float musicVolumeValue = 1.0f;
    private float fxVolumeValue = 1.0f;
    private Scores scores;

    private GameMode gameMode;

    public Menu() {

        initGUI();
    }

    public float getMusicVolumeValue() {
        return musicVolumeValue;
    }

    public float getFxVolumeValue() {
        return fxVolumeValue;
    }

    public boolean isStarted() {
        return started;
    }

    public void setStarted(boolean started) {
        this.started = started;
    }

    public boolean isReset() {
        return reset;
    }

    public void setReset(boolean reset) {
        this.reset = reset;
    }

    public GameMode getGameMode() {
        return gameMode;
    }

    public void setGameMode(GameMode gameMode) {
        this.gameMode = gameMode;
    }

    protected void initGUI() {

        display = new Display(new LWJGLBinding());
        DisplaySystem.getDisplaySystem().getRenderer().setBackgroundColor(ColorRGBA.orange);
        input = new FengJMEInputHandler(display);

        menu = new Container(new RowLayout(true));
        menu.getAppearance().setPadding(new Spacing(10, 10));
        menu.setLayoutManager(new RowLayout(false));

        display.addWidget(menu);
        initButtons(menu, display);
        buildMainMenu(menu, display);
        display.layout();
    }



    protected void initButtons(final Container c, final Display d) {

        String path = "data/menu/";

        // play button
        play = new GameMenuButton(path.concat("play.png"), path.concat("play.png"));
        play.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                buildGameModeMenu(menu, display);
            }
        });

        // options button
        options = new GameMenuButton(path.concat("options.png"), path.concat("options.png"));
        options.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                buildOptionsMenu(menu, display);
            }
        });

        // highscores button
        highscores = new GameMenuButton(path.concat("highscores.png"), path.concat("highscores.png"));
        highscores.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                buildHighscores(menu, display);
            }
        });

        // help button
        help = new GameMenuButton(path.concat("help.png"), path.concat("help.png"));
        help.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                buildHelpMenu(menu, display);
            }
        });

        // quit
        quit = new GameMenuButton(path.concat("quit.png"), path.concat("quit.png"));
        quit.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent e) {
                System.exit(0);
            }
        });

        // game mode
        gamemode = new GameMenuButton(path.concat("game-mode.png"), path.concat("game-mode.png"));

        // time attack
        timeattack = new GameMenuButton(path.concat("time-attack.png"), path.concat("time-attack.png"));
        timeattack.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent e) {
                gameMode = GameMode.TIME_ATTACK;
                setReset(true);
                setActive(false);
            }
        });

        // flags
        flags = new GameMenuButton(path.concat("flags.png"), path.concat("flags.png"));
        flags.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent e) {
                gameMode = GameMode.LAPS;
                setReset(true);
                setActive(false);
            }
        });

        // resume
        resume = new GameMenuButton(path.concat("resume.png"), path.concat("resume.png"));
        resume.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                setActive(false);
            }
        });

        // new game
        newgame = new GameMenuButton(path.concat("new-game.png"), path.concat("new-game.png"));
        newgame.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                setReset(false);
                buildGameModeMenu(menu, display);
            }
        });

        // options
        optionstit = new GameMenuButton(path.concat("options-tit.png"), path.concat("options-tit.png"));
        music = new GameMenuButton(path.concat("music.png"), path.concat("music.png"));
        fx = new GameMenuButton(path.concat("fx.png"), path.concat("fx.png"));
        musicVolume = FengGUI.createSlider(true);
        musicVolume.setHeight(20);
        musicVolume.setShrinkable(false);
        musicVolume.setValue(1.0);
        musicVolume.getAppearance().add(new PlainBackground(new Color(64, 32, 28)));
        musicVolume.addSliderMovedListener(new ISliderMovedListener()
        {
            public void sliderMoved(SliderMovedEvent sliderMovedEvent)
            {
                musicVolumeValue = (float) (sliderMovedEvent.getPosition());
            }
        });

        fxVolume = FengGUI.createSlider(true);
        fxVolume.setHeight(20);
        fxVolume.setShrinkable(false);
        fxVolume.setValue(1.0);
        fxVolume.getAppearance().add(new PlainBackground(new Color(64, 32, 28)));
        fxVolume.addSliderMovedListener(new ISliderMovedListener()
        {
            public void sliderMoved(SliderMovedEvent sliderMovedEvent)
            {
                fxVolumeValue = (float) (sliderMovedEvent.getPosition());
            }
        });

        // help
        controls = new GameMenuButton(path.concat("controls.png"), path.concat("controls.png"));
        keys = new GameMenuButton(path.concat("keys.png"), path.concat("keys.png"));

        // menu
        menutit = new GameMenuButton(path.concat("menu.png"), path.concat("menu.png"));
        menutit.addButtonPressedListener(new IButtonPressedListener() {
            public void buttonPressed(ButtonPressedEvent buttonPressedEvent) {
                if (started)
                    buildInGameMenu(menu, display);
                else
                    buildMainMenu(menu, display);
            }
        });

        // highscores
        highscorestit = new GameMenuButton(path.concat("highscorestit.png"), path.concat("highscorestit.png"));

    }

    public void setActive(boolean active) {

    	
        this.active = active;

        if (!active) {
            MouseInput.get().setCursorVisible(false);
            input.setEnabled(false);
            display.setVisible(false);
            return;
        }
        else {
            MouseInput.get().setCursorVisible(true);
            display.setVisible(true);
        }

        if(first) {
            buildMainMenu(menu, display);
            first = false;
        } else {
            buildInGameMenu(menu, display);
        }

    }

    public void render() {

        display.display();
        
    }
    public void draw(Renderer r){
    	if(!r.isProcessingQueue())
		{
			r.checkAndAdd(this);
			return;
		}
		
		// drawing FengGUI
		display.display();
    }

    public boolean isActive() {

        return this.active;
    }

    private void buildMainMenu(final Container c, Display d)  {

        c.removeAllWidgets();
        c.addWidget(play);
        c.addWidget(options);
        c.addWidget(help);
        c.addWidget(quit);
        c.pack();
        StaticLayout.center(c, d);
    }

    private void buildGameModeMenu(final Container c, Display d) {

        c.removeAllWidgets();
        c.addWidget(gamemode);
        c.addWidget(timeattack);
        c.addWidget(flags);
        c.pack();
        StaticLayout.center(c, d);
    }

    private void buildInGameMenu(final Container c, Display d) {

        c.removeAllWidgets();
        c.addWidget(resume);
        c.addWidget(newgame);
        c.addWidget(options);
        c.addWidget(highscores);
        c.addWidget(help);
        c.addWidget(quit);
        c.pack();
        StaticLayout.center(c, d);
    }

    private void buildOptionsMenu(final Container c, Display d) {

        c.removeAllWidgets();
        c.addWidget(optionstit);
        c.addWidget(music);
        c.addWidget(musicVolume);
        c.addWidget(fx);
        c.addWidget(fxVolume);
        c.addWidget(menutit);
        c.pack();
        StaticLayout.center(c, d);
    }

    private void buildHelpMenu(final Container c, Display d) {

        c.removeAllWidgets();
        c.addWidget(controls);
        c.addWidget(keys);
        c.addWidget(menutit);
        c.pack();
        StaticLayout.center(c, d);
    }

    private void buildHighscores(final Container c, Display d) {

        c.removeAllWidgets();
        c.addWidget(highscorestit);
        scores = BaseGame.getScores();

        if (scores.isEmpty()) {
            String text = "No highscores have been made";
            Label label = FengGUI.createLabel(c, text, Color.WHITE);
            label.getAppearance().add(new PlainBackground(new Color(64, 32, 28)));
            label.getAppearance().setPadding(new Spacing(10,10));
        } else {
            for (Score score : scores) {
                Label label = FengGUI.createLabel(c, score.toString(), Color.WHITE);
                label.getAppearance().add(new PlainBackground(new Color(64, 32, 28)));
                label.getAppearance().setPadding(new Spacing(10,10));
            }
        }

        c.addWidget(menutit);
        c.pack();
        StaticLayout.center(c, d);
    }

    public void setNewGameListener(IButtonPressedListener event) {

        timeattack.addButtonPressedListener(event);
        flags.addButtonPressedListener(event);
        resume.addButtonPressedListener(event);
    }
    

    public void setAlpha(float a){
//    	for (IWidget w : menu.getWidgets()) {
//    		
//		}
    }
}
